//
//  HomeCell.m
//  XKBaseDemo
//
//  Created by xukai on 2017/10/20.
//  Copyright © 2017年 xukai. All rights reserved.
//

#import "HomeCell.h"

@interface HomeCell ()

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UIButton *conView;

@end

@implementation HomeCell

-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.contentView.backgroundColor = ColorFromRGB(0xeeeeee);

        
        _conView = [[UIButton alloc] init];
        _conView.userInteractionEnabled = NO;
        [self.contentView addSubview:_conView];
//        [_conView addTarget:self action:@selector(conTouchBegin:) forControlEvents:(UIControlEventTouchDown)];
//        [_conView addTarget:self action:@selector(conTouchEnd:) forControlEvents:(UIControlEventTouchUpInside)];
        
        _imgView = [[UIImageView alloc] init];
        [_conView addSubview:_imgView];
        ViewRadius(_imgView, 10);
        
        _nameLab = [[UILabel alloc] initWithFrame:CGRectZero];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.textColor = [UIColor whiteColor];
        _nameLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:40];
        [_conView addSubview:_nameLab];
        
    }
    return self;
}

-(void) conTouchBegin
{

    CABasicAnimation *baseAnimate = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    baseAnimate.duration = 0.4;
    baseAnimate.fillMode = kCAFillModeForwards;
    baseAnimate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];//
    baseAnimate.removedOnCompletion = NO;
    baseAnimate.fromValue = [NSNumber numberWithFloat:1.0]; // 开始时的倍率
    baseAnimate.toValue = [NSNumber numberWithFloat:0.9]; // 结束时的倍率
    [_conView.layer addAnimation:baseAnimate forKey:@"1"];
    
}

-(void) conTouchEnd:(UIButton *) btn
{
    
    CABasicAnimation *baseAnimate = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    baseAnimate.duration = 0.4;
    baseAnimate.fillMode = kCAFillModeForwards;
    baseAnimate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];//
    baseAnimate.removedOnCompletion = NO;
    baseAnimate.fromValue = [NSNumber numberWithFloat:0.9]; // 开始时的倍率
    baseAnimate.toValue = [NSNumber numberWithFloat:1]; // 结束时的倍率
    [btn.layer addAnimation:baseAnimate forKey:@"2"];
    
    if([self.delegate respondsToSelector:@selector(homeCellTap:)])
    {
        [self.delegate homeCellTap:self.model];
    }

}

-(void) layoutSubviews
{
    
    [super layoutSubviews];
    _conView.frame = CGRectMake(20, 20, ScreenW - 40, 380);
    _imgView.frame = CGRectMake(0, 0, _conView.width, _conView.height);
    _nameLab.frame = CGRectMake(40, 50, _imgView.width, 50);
    _nameLab.bottom = _imgView.height - 20;
}

-(void) setModel:(HomeCellModel *)model
{
    _model = model;
    _imgView.image = [UIImage imageNamed:model.icon];
    _nameLab.text = model.title;
}
@end
